<?php 
require_once "includes/header.php";
require_once "core/initialize.php";

	if (Input::exists()) {
			
		if (Token::check(Input::get('token'))) {
			
			 	//echo "this has been run </br>";

			$validate = new Validation();
			$validation = $validate->check($_POST, array(

				'firstname' => array(
					'required' =>  true,
					'min' => 2,
					'max' => 50
				),
				'lastname' => array(
					'required' =>  true,
					'min' => 2,
					'max' => 50
				),
				'department' => array(
					'required' => true,
					'min' => 10,
					'max' =>50
				),
				'email' => array(
					'required' => true,
					'min' => 2,
					'max' => 255,
					'unique' => 'users'
				),

				'username' => array(
					'required' => true,
					'min' => 2,
					'max' => 20,
					'unique' => 'users'
				),

				'reg_password' => array(
					'required' =>  true,
					'min' => 6
				),

				'retype_reg_password' => array(
					'required' =>  true,
					'matches' => 'reg_password'
				)

			));

			if ($validation->passed()) {
	
				$user = new User();

				$salt = Hash::salt(32);

				try {

					$user->create(array(
						'firstname' => Input::get('firstname'),
						'lastname' => Input::get('lastname'),
						'email' => Input::get('email'),
						'activation' => Hash::make(Input::get('email').time()),
						'username' => Input::get('username'),
						'department' => Input::get('department'),
						'password' =>  Hash::make(Input::get('reg_password'), $salt),
						'salt' => $salt,
						'joined' => date('Y-m-d H:i:s'),
						'group' => 1
					));

					
					Session::flash('home', '<div class="column div_1_of_1 positive"><p>you registered successfully! <br/> Please check '. Input::get('email') . ' for activation link. </p></div>');
					Redirect::to('register.php');
					
				} catch (Exception $e) {
					die($e->getMessage());

				}


				//register user
			}else {
				// if run some problems show errors
				//print_r($validation->errors());
				foreach ($validation->errors() as $error) {
					echo "<div class='input_error'>" . $error  . "</div>";
				}
			}

		}

	}

?>

<div class="column div_1_of_2 componentDesignA">
	 <div class="section clearing">
	 	<div class="column div_1_of_1 widgetHeaderTheme">
			<h1>Some Basic information here.</h1><br/>
			<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look</p>
		</div>
	 </div>
</div>	

<div class="column div_1_of_2 componentDesignA">

	<form action="" method="post">
		<div class="section clearing">

			<?php
				if (Session::exists('home')) {
				echo Session::flash('home');
				}
		 	?>

			<div class="column div_1_of_1 widgetHeaderTheme">
				<h1>Sign up</h1>
			</div>
		</div>	
		<div class="section clearing">
			<div class="column div_1_of_2 widgetTransBgTheme">
				<input type="text" name="firstname" value="<?php  echo escape(Input::get('firstname')); ?>" placeholder="First name"/>
			</div>
			<div class="column div_1_of_2 widgetTransBgTheme">
				<input type="text" name="lastname" value="<?php  echo escape(Input::get('lastname')); ?>" placeholder="Last name"/>
			</div>
		</div>
		<div class="section clearing">
			<div class="column div_1_of_1 widgetTransBgTheme">
				<input  type="text" name="department" value="<?php  echo escape(Input::get('department')); ?>" placeholder="Department name"/>
			</div>
		</div>
		<div class="section clearing">
			<div class="column div_1_of_1 widgetTransBgTheme">
				<input type="text" name="email" value="<?php  echo escape(Input::get('email')); ?>" placeholder="Email Address"/>
			</div>
		</div>
		<div class="section clearing">
			<div class="column div_1_of_1 widgetTransBgTheme">
				<input type="text" name="username" value="<?php  echo escape(Input::get('username')); ?>" placeholder="Username"/>
			</div>
		</div>
		<div class="section clearing">
			<div class="column div_1_of_1 widgetTransBgTheme">
				<input type="password" name="reg_password" placeholder="Password"/>
			</div>
		</div>
		<div class="section clearing">
			<div class="column div_1_of_1 widgetTransBgTheme">
				<input type="password" name="retype_reg_password" placeholder="Re-type Password"/>
			</div>
		</div>
		<div class="section clearing">
			<div class="column div_1_of_1">
				<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
				<input type="submit" name="submit" value="Sign up"/>
			</div>
		</div>
	</form><!-- END OF SIGN UP FORM -->		
</div>
<?php require_once "includes/footer.php"; ?>